<?php

namespace app\admin\command;

use app\admin\model\wanlshop\Goods;
use app\api\service\wanlshop\Common;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Log;

class Downs extends Command
{
    protected function configure()
    {
        $this->setName('downs')
            ->setDescription('商品上下架');
    }

    protected function execute(Input $input, Output $output)
    {
        Goods::where('status', 'normal')->chunk(1000, function ($goods) use ($output) {
            foreach ($goods as $item) {
                $st = microtime(true);
                if(empty($item['c_father_goods_id'])){
                    continue;
                }
                $res = Common::getData('getGoodsDetails', ['father_id' => $item['c_father_goods_id']]);
                $g = Goods::find($item['id']);
                if ($res) {
                    $s = strtotime($res['c_start_datetime']);
                    $e = strtotime($res['c_stop_datetime']);
                    if ($res['c_is_show'] != 1 || time() < $s || time() > $e) {
                        $g->status = 'hidden';
                        $g->c_start_datetime = $res['c_start_datetime'];
                        $g->c_stop_datetime = $res['c_stop_datetime'];
                        $g->save();
                        $ed = microtime(true);
                        $ex = bcsub($ed, $st, 4);
                        $msg = "{$g['id']} - {$g['title']} - {$g['c_father_goods_id']} --- 下架 ,耗时:$ex 秒";
                        Log::log($msg);
                        $output->writeln($msg);
                    } else {
                        $g->c_start_datetime = $res['c_start_datetime'];
                        $g->c_stop_datetime = $res['c_stop_datetime'];
                        $g->save();
                        $ed = microtime(true);
                        $ex = bcsub($ed, $st, 4);
                        $msg = "{$res['c_goods_name']} --- {$g['c_father_goods_id']} - 跳过  耗时：$ex 秒";
                        $output->writeln($msg);
                    }
                } else {
                    $ed = microtime(true);
                    $ex = bcsub($ed, $st, 4);
                    $g->status = 'hidden';
                    $g->save();
                    $msg = "{$item['title']} --- 不存在下架  耗时：$ex 秒";
                    $output->writeln($msg);
                }
            }
        });
    }
}